﻿#include "GameCameraController.h"
#include<iostream>
GameCameraController::GameCameraController()
{
}

GameCameraController::~GameCameraController()
{
}

void GameCameraController::onCursor(double xpos, double ypos)
{
	float deltaX = mSensitive * (xpos - mCurrentX);
	float deltaY = mSensitive * (ypos - mCurrentY);
	if (mLeftMouseDown) {
		//摄像机位置不动，只是上下改变视角
		pitch(deltaY);
		yaw(deltaX);
	}
	mCurrentX = xpos;
	mCurrentY = ypos;
}

void GameCameraController::onScroll(float offset)
{
}

void GameCameraController::update()
{
	//最终移动方向
	glm::vec3 direction(0.0f);
	auto front = glm::cross(mCamera->mUp, mCamera->mRight);
	auto right = mCamera->mRight;
	if (mKeyMap[GLFW_KEY_W] == true) {
		direction += front;
	}
	if (mKeyMap[GLFW_KEY_S] == true) {
		direction -= front;
	}
	if (mKeyMap[GLFW_KEY_D] == true) {
		direction -= right;
	}
	if (mKeyMap[GLFW_KEY_A] == true) {
		direction += right;
	}
	if (glm::length(direction) != 0) {
		direction = glm::normalize(direction);
		mCamera->mPosition += direction * mSpeed;
	}
}

void GameCameraController::pitch(float angle)
{
	mPitch += angle;
	if (mPitch > 89.0f) {
		mPitch -= angle;
		return;
	}
	glm::mat4 mat = glm::rotate(glm::mat4(1.0), glm::radians(angle), mCamera->mRight);
	mCamera->mUp = mat * glm::vec4(mCamera->mUp,0.0);
}

void GameCameraController::yaw(float angle)
{
	glm::mat4 mat = glm::rotate(glm::mat4(1.0), glm::radians(angle), glm::vec3(0.0, 1.0, 0.0));
	mCamera->mRight = mat * glm::vec4(mCamera->mRight,0.0);
	mCamera->mUp = mat * glm::vec4(mCamera->mUp, 0.0);

}
